package com.ymatou.iapi.livebuyer.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryBySellerHomeBean;
import com.ymatou.iapi.livebuyer.service.QueryBySellerHomeCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.livevideo.parameter.RecLiveNotifyBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*买手主页露出互动直播信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryBySellerHome{
	private static QueryBySellerHomeBean querybysellerhomeBean;
	private static QueryBySellerHomeCall querybysellerhomeCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手主页露出互动直播信息");
	}

	@Before
	public void caseUp() {
		querybysellerhomeBean=new QueryBySellerHomeBean();
		querybysellerhomeCall=new QueryBySellerHomeCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querybysellerhomeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(LiveRoomInfo liveRoomInfo) throws Exception {
		LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveRoomInfo.getLiveId());
		Logger.verifyEquals(liveroom.getStartTime().substring(0, 19), liveRoomInfo.getStartTime(), "验证StartTime");
		Logger.verifyEquals(liveroom.getCreateTime().substring(0, 19), liveRoomInfo.getCreateTime(), "验证CreateTime");
		Logger.verifyEquals(liveroom.getGroupId(), liveRoomInfo.getGroupId(), "验证GroupId");
		Logger.verifyEquals(0,liveroom.getLiveType(), "验证这个接口返回的是码头直播");
		// Logger.verifyEquals(liveroom.getHistoryPlayUrl(),
		// liveRoomInfo.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(liveroom.getLiveCover(), liveRoomInfo.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(liveroom.getId(), liveRoomInfo.getLiveId(), "验证LiveId");
		// 修改status
		if (liveroom.getLiveStatus() == 0) {
			if (liveroom.getStartTime().compareTo(YMTDateUtil.getSysTime()) < 0) {
				Logger.verifyEquals(1, liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			} else {
				Logger.verifyEquals(liveroom.getLiveStatus(), liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			}
		}

		Logger.verifyEquals(liveroom.getLiveTitle(), liveRoomInfo.getLiveTitle(), "验证LiveTitle");
		//这里要用接口返回的不能用数据库的验证
		String playurl = DesUtil.decrypt(liveRoomInfo.getPlayUrl(), DesUtil.DES_KEY_STRING, DesUtil.DES_IVPARAMETER);
		// rtmp://12808.liveplay.myqcloud.com/live/12808_SIT1_4028_550
		Logger.debug("playurl：" + playurl);
		String mypurl = "http://" + LiveRoomCallService.PlayDomain + "/live/" + LiveRoomCallService.BizId + "_" + EnvSetup.getEnv().toUpperCase() + "_"
				+ liveroom.getId() + "_700.flv";
		Logger.verifyEquals(mypurl, playurl, "验证PlayUrl");
		if (liveRoomInfo.getPreVideo() != null) {
			Logger.verifyEquals(liveroom.getPreVideoHeight(), liveRoomInfo.getPreVideo().getPreVideoHeight(), "验证getPreVideoHeight");
			Logger.verifyEquals(liveroom.getPreVideoLength(), liveRoomInfo.getPreVideo().getPreVideoLength(), "验证getPreVideoLength");
			Logger.verifyEquals(liveroom.getPreVideoUrl(), liveRoomInfo.getPreVideo().getPreVideoUrl(), "验证getPreVideoUrl");
			Logger.verifyEquals(liveroom.getPreVideoWidth(), liveRoomInfo.getPreVideo().getPreVideoWidth(), "验证getPreVideoWidth");
		}
		Logger.verifyEquals(liveroom.getUserId(), liveRoomInfo.getUserId(), "验证UserId");
		Logger.verifyEquals(liveroom.getLiveDesc(), liveRoomInfo.getLiveDesc(), "验证LiveDesc");
		Logger.verifyEquals(liveroom.getPutawayCount(), liveRoomInfo.getPutawayCount(), "验证PutawayCount");
		Map livestatusmap = liveRoomWapper.selectLiveRoomStatById(liveRoomInfo.getLiveId());
		if (livestatusmap != null) {
			Logger.verifyEquals(livestatusmap.get("ViewRealNum"), liveRoomInfo.getRealViewNum(), liveRoomInfo.getLiveId() + ":验证ViewRealNum");
			Logger.verifyEquals(livestatusmap.get("PreViewNum"), liveRoomInfo.getPreViewNum(), liveRoomInfo.getLiveId() + ":验证PreViewNum");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryBySellerHome_001() {
		Logger.start(true,"买手主页露出互动直播信息成功,进行中的直播");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			querybysellerhomeBean.setSellerId(livedto.getUserId());
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();
			Logger.verifyEquals("200",querybysellerhomeCall.getString("code"),"验证code");
			Logger.verifyEquals(livedto.getId(),querybysellerhomeCall.getLiveRoomInfo().getLiveId(),"验证露出id");
			verifyDB(querybysellerhomeCall.getLiveRoomInfo());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryBySellerHome_002() {
		Logger.start(true,"不输入sellerid失败");
		try {
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();			
			Logger.verifyEquals("100",querybysellerhomeCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryBySellerHome_003() {
		Logger.start(true,"输入sellerid没有直播信息返回空");
		try {
			querybysellerhomeBean.setSellerId(55);
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();			
			Logger.verifyEquals("200",querybysellerhomeCall.getString("code"),"验证code");
			Logger.verifyEquals(null,querybysellerhomeCall.getLiveRoomInfo(),"验证露出id");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryBySellerHome_004() {
		Logger.start(true,"买手主页露出互动直播信息成功,预告");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(livedto.getId(), 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			
			CreateSellerLiveBean initcreatebean2 = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto2 = LiveRoomCallService.CreateSellerLive(initcreatebean2);
			
			querybysellerhomeBean.setSellerId(livedto.getUserId());
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();
			Logger.verifyEquals("200",querybysellerhomeCall.getString("code"),"验证code");
			Logger.verifyEquals(livedto2.getId(),querybysellerhomeCall.getLiveRoomInfo().getLiveId(),"验证露出id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryBySellerHome_005() {
		Logger.start(true,"买手主页露出互动直播信息成功,回播");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(livedto.getId(), 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			Thread.sleep(1000);
			querybysellerhomeBean.setSellerId(livedto.getUserId());
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();
			Logger.verifyEquals("200",querybysellerhomeCall.getString("code"),"验证code");
			Logger.verifyEquals(livedto.getId(),querybysellerhomeCall.getLiveRoomInfo().getLiveId(),"验证露出id");
			verifyDB(querybysellerhomeCall.getLiveRoomInfo());	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryBySellerHome_006() {
		Logger.start(true,"买手主页露出互动直播信息成功,不会露出没有回播地址的回播");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			LiveRoomCallService.AutoCloseLiveRoomByUserId(livedto.getUserId());
//			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(livedto.getId(), 100);
//			LiveRoomCallService.LiveNotify(recivenotifyBean);
			
			querybysellerhomeBean.setSellerId(livedto.getUserId());
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();
			Logger.verifyEquals("200",querybysellerhomeCall.getString("code"),"验证code");
//			Logger.verifyNotEquals(livedto.getId(),querybysellerhomeCall.getLiveRoomInfo().getLiveId(),"验证露出id");
			Logger.verifyEquals(null,querybysellerhomeCall.getLiveRoomInfo(),"验证露出id");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	// http://172.16.100.245//PRDList/买手主页需求-0426/index.html#g=1&p=买手主页直播_内容修改需求点
	 // 历史直播不露出
    // 预告只露出近二天
	public void Tc_QueryBySellerHome_007() {
		Logger.start(true,"买手主页露出互动直播信息-历史直播不露出，预告只露出近二天");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(livedto.getId(), 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			
			CreateSellerLiveBean initcreatebean2 = LiveRoomCallService.initCreatesellerliveBean(null);
			initcreatebean2.getLiveDto().setStartTime(YMTDateUtil.getBeforeOrNextMunite(2*24*60+10));
			LiveRoom livedto2 = LiveRoomCallService.CreateSellerLive(initcreatebean2);
			
			querybysellerhomeBean.setSellerId(livedto.getUserId());
			querybysellerhomeCall.setData(querybysellerhomeBean);
			querybysellerhomeCall.callService();
			Logger.verifyEquals("200",querybysellerhomeCall.getString("code"),"验证code");
			Logger.verifyEquals(null,querybysellerhomeCall.getLiveRoomInfo(),"验证露出id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}